package com.zxm.sunshineaiary.view.myedittext;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;

import com.zxm.sunshineaiary.activity.R;

public class MyRect extends View implements AnimationListener {

	private Context mContext = null;
	private Animation selectAnimationTrue_Alpha = null;
	private Animation selectAnimationFlase_Alpha = null;
	private Animation selectAnimationStart_Alpha = null;
	private RectF mRectF = null;
	private Paint mPaint = null;
	private SweepGradient mGradBkg = null;
	private SweepGradient mGradBkgTouch = null;
	private boolean select = false;
	private int viewColor = 0;
	private int viewGradientColor = 0;

	public MyRect(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		createAnimation();
	}

	public MyRect(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		createAnimation();
	}

	public MyRect(Context context, int viewColor) {
		super(context);
		mContext = context;
		createAnimation();
		this.viewColor = viewColor;
	}

	public MyRect(Context context, int viewColor, int viewGradientColor) {
		super(context);
		this.viewColor = viewColor;
		this.viewGradientColor = viewGradientColor;
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		mRectF = new RectF(0, 0, w, h);
		intit();
		super.onSizeChanged(w, h, oldw, oldh);
	}

	private void intit() {
		mPaint = new Paint();
		mGradBkg = new SweepGradient(mRectF.top, mRectF.bottom,
				viewGradientColor, viewColor);
		mGradBkgTouch = new SweepGradient(mRectF.bottom, mRectF.top, viewColor,
				viewGradientColor);
		mPaint.setShader(mGradBkg);
		mPaint.setAntiAlias(true);
		mPaint.setAlpha(102);
	}

	public int getViewColor() {
		return viewColor;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawRoundRect(mRectF, 5, 5, mPaint);
		super.onDraw(canvas);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			Log.v("xxx", "ACTION_DOWN");
			mPaint.setShader(mGradBkgTouch);
			invalidate();
		}
		if (event.getAction() == MotionEvent.ACTION_UP) {
			Log.v("xxx", "ACTION_UP");
			mPaint.setShader(mGradBkg);
			invalidate();
		}
		return true;
	}

	public void setPaintShader(){
		
	}
	
	public void selectOn() {
		if (!select) {
			select = true;
			mPaint.setAlpha(255);
			this.startAnimation(selectAnimationTrue_Alpha);
		}
	}

	public void selectOf() {
		if (select) {
			select = false;
			this.startAnimation(selectAnimationFlase_Alpha);
		}
	}

	private void createAnimation() {
		selectAnimationTrue_Alpha = AnimationUtils.loadAnimation(mContext,
				R.anim.my_rect_color_true);

		selectAnimationFlase_Alpha = AnimationUtils.loadAnimation(mContext,
				R.anim.my_rect_color_false);
		selectAnimationFlase_Alpha.setFillAfter(true);
		selectAnimationFlase_Alpha.setAnimationListener(this);

		selectAnimationStart_Alpha = AnimationUtils.loadAnimation(mContext,
				R.anim.my_rect_color_start);

	}

	@Override
	public void onAnimationStart(Animation animation) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onAnimationEnd(Animation animation) {
		
	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		// TODO Auto-generated method stub

	}
}
